package com.kksoft.readcm.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;

import com.kksoft.readcm.a.Appconfig;
import com.kksoft.readcm.pro.R;

public class SettingsAdvActivity extends PreferenceActivity
{
  ListPreference a = null;
  CheckBoxPreference b = null;
  private Preference c = null;
 // private Preference d = null;
  private CheckBoxPreference e = null;
  //private Context f;
  private String g;

  public final void a(String paramString)
  {
    this.g = paramString;
  }

  protected void onCreate(Bundle bundle)
  {
	  super.onCreate(bundle);
      Window window = getWindow();
      Display display = getWindowManager().getDefaultDisplay();
      int i = display.getWidth();
      int j = display.getHeight();
      float f1 = i / 2;
      float f2 = j / 2;
      float f3;
      float f4;
      RectShape rectshape;
      ShapeDrawable shapedrawable;
      Paint paint;
      int i1;
      int j1;
      android.graphics.Shader.TileMode tilemode;
      RadialGradient radialgradient;      
      if(f1 > f2)
          f3 = f1;
      else
          f3 = f2;
      f4 = (float)((double)f3 * 0.86D);
      rectshape = new RectShape();
      shapedrawable = new ShapeDrawable(rectshape);
      shapedrawable.setDither(true);
      paint = shapedrawable.getPaint();
      i1 = Color.parseColor("#424141");
      j1 = Color.parseColor("#313131");
      tilemode = android.graphics.Shader.TileMode.CLAMP;
      radialgradient = new RadialGradient(f1, f2, f4, i1, j1, tilemode);
      paint.setShader(radialgradient);
      window.setBackgroundDrawable(shapedrawable);
      //f = this;
      addPreferencesFromResource(R.xml.adv_settings);
  
      c = findPreference("clear_cache");
      c.setOnPreferenceClickListener(new OnPreferenceClickListener(){

		@Override
		public boolean onPreferenceClick(Preference preference) {
			// TODO Auto-generated method stub
			SettingsAdvActivity.this.showDialog(1);
		    return true;
		}
    	  
      });
      
      a = (ListPreference)findPreference("backgroundSyncingString");
      a.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

    		@Override
    		public boolean onPreferenceChange(Preference preference, Object newValue) {
    			// TODO Auto-generated method stub
    		    return App.v().a((String)newValue, SettingsAdvActivity.this, SettingsAdvActivity.this.a);
    		}
          });
      
      a.setSummary(App.v().getSyncSum());
     
      e = (CheckBoxPreference)findPreference("mobileAgent");
      e.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

  		@Override
  		public boolean onPreferenceChange(Preference preference, Object newValue) {
  			// TODO Auto-generated method stub
  			App.a(((Boolean)newValue).booleanValue());
  		    return true;
  		}
        });
      
      b = (CheckBoxPreference)findPreference("useSdCard");
      b.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			// TODO Auto-generated method stub
		    if (((Boolean)newValue).booleanValue())
		    {
		      String str = Environment.getExternalStorageState();
		      if ("mounted".equals(str))
		      {
		    	  SettingsAdvActivity.this.showDialog(3);
		    	  return true;
		      }
		      else
		      {
		    	  SettingsAdvActivity.this.showDialog(5);
		    	  return false;
		      }
		    }
		    else
		    {
		    	SettingsAdvActivity.this.showDialog(4);
		    	  return false;
		    }
		}
    	  
      });
      
      getListView().setCacheColorHint(0);
  }

  protected Dialog onCreateDialog(int paramInt)
  {
	  //Dialog dialog;
    switch (paramInt)
    {
    default:
    	return null;
      //break;
    case 1:
    	 AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	 builder.setTitle(getString(R.string.dg_confirm_cache_clear_t))
    	 		.setMessage(getString(R.string.dg_confirm_cache_clear_m))
    	 		.setPositiveButton(getString(R.string.ac_yes), new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
						SettingsAdvActivity.this.showDialog(2);
//					    e locale = Appconfig.q().c();
//					    q q1 = new q(SettingsAdvActivity.this);
//					    locale.a(false, q1);
						//Appconfig.getThis().getItemAppObservable().a(false,new q(SettingsAdvActivity.this));
						Appconfig.getThis().getItemAppObservable().a(false,new com.kksoft.readcm.c.e(){

							@Override
							public void a() {
								// TODO Auto-generated method stub
								SettingsAdvActivity.this.removeDialog(2);
							}});
					}
    	 			
    	 		})
    	 		.setNegativeButton(getString(R.string.ac_cancel),new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
    	 			
    	 		});
    	 Dialog dialog = builder.create();
    	 dialog.setCanceledOnTouchOutside(true);
    	 return dialog;
    	//break;
    case 2:
    	ProgressDialog pDialog = new ProgressDialog(this);
    	pDialog.setMessage(getString(R.string.dg_clearing_cache));
    	pDialog.setIndeterminate(true);
    	pDialog.setCancelable(false);
        return pDialog;
    	//break;
    case 3:
    	 
    	 AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
    	 builder1.setTitle(getString(R.string.dg_sdcard_confirm_change_t))
    	 		.setMessage(getString(R.string.dg_sdcard_confirm_change_m))
    	 		.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						SettingsAdvActivity.this.b.setChecked(true);
					    App.setUseSDCard(true, SettingsAdvActivity.this);
					    dialog.dismiss();
					}
    	 			
    	 		})
    	 		.setNegativeButton(getString(R.string.ac_cancel), new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
    	 			
    	 		});
    	 		
         
         
    	 Dialog dialog1=builder1.create();
    	 dialog1.setCanceledOnTouchOutside(true);
    	 return dialog1;
    	//break;
    case 4:
    	AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
    	builder2.setTitle(getString(R.string.dg_sdcard_confirm_change_t))
    			.setMessage(getString(R.string.dg_sdcard_confirm_change_m))
    			.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						SettingsAdvActivity.this.b.setChecked(false);
					    App.setUseSDCard(false, SettingsAdvActivity.this);
					    dialog.dismiss();
					}
    	 			
    	 		})
    	 		.setNegativeButton(getString(R.string.ac_cancel), new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
    	 			
    	 		});
    	Dialog dialog2=builder2.create();
   	 	dialog2.setCanceledOnTouchOutside(true);
   	 	return dialog2;
    case 5:
    	AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
    	builder3.setTitle(getString(R.string.dg_sdcard_not_available_t))
		.setMessage(getString(R.string.dg_sdcard_not_available_settings_m))
		.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			}
 			
 		});
    	Dialog dialog3=builder3.create();
    	dialog3.setCanceledOnTouchOutside(true);
    	return dialog3;
    case 6:
    	ProgressDialog pDialog1 = new ProgressDialog(this);
    	pDialog1.setMessage(getString(R.string.dg_changing_data_location));
    	pDialog1.setIndeterminate(true);
    	pDialog1.setCancelable(false);
    	return pDialog1;
    case 7:
    	ProgressDialog pDialog2 = new ProgressDialog(this);
        pDialog2.setMessage(getString(R.string.dg_c2dm_registering));
        pDialog2.setIndeterminate(true);
        pDialog2.setCancelable(false);
        return pDialog2;
    case 8:
    	AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
    	builder4.setTitle(getString(R.string.dg_c2dm_unsupported_t))
		.setMessage(getString(R.string.dg_c2dm_unsupported_m))
		.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			}
 			
 		});
    	Dialog dialog4=builder4.create();
    	dialog4.setCanceledOnTouchOutside(true);
    	return dialog4;
    case 9:
    	AlertDialog.Builder builder5 = new AlertDialog.Builder(this);
    	builder5.setTitle(getString(R.string.dg_c2dm_failed_t))
		.setMessage(getString(R.string.dg_c2dm_failed_general_m))
		.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			}
 			
 		});
    	Dialog dialog5=builder5.create();
    	dialog5.setCanceledOnTouchOutside(true);
    	return dialog5;
    case 10:
    	AlertDialog.Builder builder6 = new AlertDialog.Builder(this);
    	builder6.setTitle(getString(R.string.dg_c2dm_failed_t))
		.setMessage(getString(R.string.dg_c2dm_not_connected_m))
		.setPositiveButton(getString(R.string.ac_ok), new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
			    dialog.dismiss();
			}
 			
 		});
    	Dialog dialog6=builder6.create();
    	dialog6.setCanceledOnTouchOutside(true);
    	return dialog6;
    }
  }

  public boolean onCreateOptionsMenu(Menu menu)
  {
	  menu.add(-1, 6, 6, getString(R.string.mu_help)).setIcon(R.drawable.ic_menu_help);
      return super.onCreateOptionsMenu(menu);
  }

  public boolean onOptionsItemSelected(MenuItem menuitem)
  {
	  boolean flag;
	    switch (menuitem.getItemId())
	    {
	    default:
	    	flag = super.onOptionsItemSelected(menuitem);
	    	break;
	    case 6:
	    	Context context = App.getAppThis();
	        Intent intent = new Intent(context, HelpActivity.class);
	        startActivity(intent);
	        flag = true;
	    	break;
	    }
	    return flag;
  }

  public void onPause()
  {
    App.setWarkful(3, null);
    super.onPause();
  }

  public void onResume()
  {
    Appconfig.getThis().o();
    App.setWarkful(1, null);
    super.onResume();
  }

  public boolean onSearchRequested()
  {
    return false;
  }
}